﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
namespace MyBarcodeGenerator;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
    public MainWindow() {
        InitializeComponent();
    }
    //判断输入的字符串中是否包含中文字符
    private static string pattern = @"[\u4e00-\u9fa5]";
    private static Regex regex = new Regex(pattern);
    private void PublicLogic() {
        var text = TextBox.Text;
        if (text == "") {
            CodeImage.Source = null;
            return;
        }
        //判断输入的字符串中是否包含中文字符
        if (regex.IsMatch(text)) {
            MessageBox.Show("暂不支持中文字符", "提示");
            TextBox.Text = string.Empty;
            return;
        }
        if (CheckBox.IsChecked == true) {
            text = text + '\n';
        }
        if (BarCodeRadioButton.IsChecked == true) {
            CodeImage.Source = BarcodeGenerator.GenerateBarcode(text, BarcodeFormat.CODE_128);
        }
        if (QrRadioButton.IsChecked == true) {
            CodeImage.Source = BarcodeGenerator.GenerateQrCode(text);
        }
    }
    private void TextBox_OnTextChanged(object sender, TextChangedEventArgs e) {
        PublicLogic();
    }

    private void RadioButton_OnClick(object sender, RoutedEventArgs e) {
        PublicLogic();
    }
    private void CheckBox_OnClick(object sender, RoutedEventArgs e) {
        PublicLogic();
    }
    private void ButtonBase_OnClick(object sender, RoutedEventArgs e) {
        TextBox.Text = string.Empty;
    }
}
public class BarcodeGenerator {
    //private static BarcodeGenerator generator;
    private static BarcodeWriterPixelData writer;
    private BarcodeGenerator(){}
    /*public static BarcodeGenerator GetGenerate() {
        return generator == null ? new BarcodeGenerator() : generator;
    }*/
    private static BarcodeWriterPixelData getWriter(int flag, BarcodeFormat format) {
        if (flag == 0) {
            writer = new BarcodeWriterPixelData {
                Format = format,
                Options = new QrCodeEncodingOptions {
                    Height = 300,
                    Width = 300,
                    Margin = 1
                }
            };
        }else if (flag == 1) {
            writer = new BarcodeWriterPixelData {
                Format = format,
                Options = new EncodingOptions {
                    Height = 100,
                    Width = 300,
                    Margin = 2,
                    PureBarcode = true
                }
            };
        }
        return writer;
    }
    // 生成QR码
    public static BitmapSource GenerateQrCode(string content) {
        writer = getWriter(0, BarcodeFormat.QR_CODE);
        var pixelData = writer.Write(content);
        return BitmapSource.Create(
            pixelData.Width, pixelData.Height,
            96, 96, // DPI
            System.Windows.Media.PixelFormats.Bgra32,
            null,
            pixelData.Pixels,
            pixelData.Width * 4);
    }

    // 生成条形码
    public static BitmapSource GenerateBarcode(string content, BarcodeFormat format) {
        writer = getWriter(1, format);
        var pixelData = writer.Write(content);
        return BitmapSource.Create(
            pixelData.Width, pixelData.Height,
            96, 96,
            System.Windows.Media.PixelFormats.Bgra32,
            null,
            pixelData.Pixels,
            pixelData.Width * 4);
    }
}